package com.whmall.service.crm.module.companybusinesslicense.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.whmall.component.util.DateUtils;
import com.whmall.component.util.FieldUtils;
import com.whmall.service.crm.entity.dto.companybusinesslicense.BusinessLicenseQueryReqDTO;
import com.whmall.service.crm.entity.dto.companybusinesslicense.CompanyBusinessLicenseRspDTO;
import com.whmall.service.crm.entity.dto.companybusinesslicense.CreateCompanyBusinessLicenseReqDTO;
import com.whmall.service.crm.entity.dto.companybusinesslicense.UpdateCompanyBusinessLicenseReqDTO;
import com.whmall.service.crm.module.companybusinesslicense.dao.CompanyBusinessLicenseMapper;
import com.whmall.service.crm.module.companybusinesslicense.entity.CompanyBusinessLicenseDO;
import com.whmall.service.crm.module.companybusinesslicense.service.CompanyBusinessLicenseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author：yangdong
 * Date：2020/8/3
 * Description：营业执照业务层实现
 */
@Service
public class CompanyBusinessLicenseServiceImpl implements CompanyBusinessLicenseService {

    @Autowired
    private CompanyBusinessLicenseMapper businessLicenseMapper;

    @Override
    public List<CompanyBusinessLicenseRspDTO> selectBy(BusinessLicenseQueryReqDTO queryReqDTO) {
        List<CompanyBusinessLicenseDO> companyBusinessLicenseDOList = businessLicenseMapper.selectByReqDTO(queryReqDTO);
        List<CompanyBusinessLicenseRspDTO> list = new ArrayList<>();
        for (CompanyBusinessLicenseDO companyBusinessLicenseDO : companyBusinessLicenseDOList) {
            CompanyBusinessLicenseRspDTO rspDTO = new CompanyBusinessLicenseRspDTO();
            BeanUtils.copyProperties(companyBusinessLicenseDO, rspDTO);
            if (companyBusinessLicenseDO.getEffective().equals(1)) {
                rspDTO.setIsEnabled(true);
            }else {
                rspDTO.setIsEnabled(false);
            }
            rspDTO.setBusinessStartDate(DateUtils.filterDate(
                    rspDTO.getBusinessStartDate()));
            rspDTO.setBusinessEndDate(DateUtils.filterDate(
                    rspDTO.getBusinessEndDate()));
            list.add(rspDTO);
        }
        return list;
    }

    @Override
    public CompanyBusinessLicenseRspDTO getById(Long id) {
        CompanyBusinessLicenseDO companyBusinessLicenseDO = businessLicenseMapper.getById(id);
        CompanyBusinessLicenseRspDTO companyBusinessLicenseRspDTO = new CompanyBusinessLicenseRspDTO();
        BeanUtils.copyProperties(companyBusinessLicenseDO, companyBusinessLicenseRspDTO);
        if (companyBusinessLicenseDO.getEffective().equals(1)) {
            companyBusinessLicenseRspDTO.setIsEnabled(true);
        }else {
            companyBusinessLicenseRspDTO.setIsEnabled(false);
        }
        if (FieldUtils.DEFAULT_DATE.equals(companyBusinessLicenseRspDTO.getBusinessEndDate())){
            Date endDate = DateUtil.parse("9999-12-31");
            companyBusinessLicenseRspDTO.setBusinessEndDate(endDate);
        }
        return companyBusinessLicenseRspDTO;
    }

    @Override
    public int insert(CreateCompanyBusinessLicenseReqDTO createCompanyBusinessLicenseReqDTO) {
        CompanyBusinessLicenseDO companyBusinessLicenseDO = new CompanyBusinessLicenseDO();
        BeanUtils.copyProperties(createCompanyBusinessLicenseReqDTO, companyBusinessLicenseDO);
        if (createCompanyBusinessLicenseReqDTO.getIsEnabled()) {
            companyBusinessLicenseDO.setEffective(1);
        }else {
            companyBusinessLicenseDO.setEffective(Integer.parseInt(String.valueOf(System.currentTimeMillis()/1000)));
        }
        companyBusinessLicenseDO.setCreateType(createCompanyBusinessLicenseReqDTO.getOperatorType());
        companyBusinessLicenseDO.setCreateId(createCompanyBusinessLicenseReqDTO.getOperatorId());
        companyBusinessLicenseDO.setCreateTime(new Date());
        companyBusinessLicenseDO.setUpdateType(createCompanyBusinessLicenseReqDTO.getOperatorType());
        companyBusinessLicenseDO.setUpdateId(createCompanyBusinessLicenseReqDTO.getOperatorId());
        companyBusinessLicenseDO.setUpdateTime(new Date());
        return businessLicenseMapper.insert(companyBusinessLicenseDO);
    }

    @Override
    public int update(UpdateCompanyBusinessLicenseReqDTO updateCompanyBusinessLicenseReqDTO) {
        CompanyBusinessLicenseDO companyBusinessLicenseDO = new CompanyBusinessLicenseDO();
        BeanUtils.copyProperties(updateCompanyBusinessLicenseReqDTO, companyBusinessLicenseDO);
        if (updateCompanyBusinessLicenseReqDTO.getIsEnabled()) {
            companyBusinessLicenseDO.setEffective(1);
        }else {
            companyBusinessLicenseDO.setEffective(Integer.parseInt(String.valueOf(System.currentTimeMillis()/1000)));
        }
        companyBusinessLicenseDO.setUpdateType(updateCompanyBusinessLicenseReqDTO.getOperatorType());
        companyBusinessLicenseDO.setUpdateId(updateCompanyBusinessLicenseReqDTO.getOperatorId());
        companyBusinessLicenseDO.setUpdateTime(new Date());
        return businessLicenseMapper.update(companyBusinessLicenseDO);
    }

    @Override
    public int countEffective(Long id, Long companyId, String socialCreditCode) {
        return businessLicenseMapper.countEffective(id, companyId, socialCreditCode);
    }

    @Override
    public void revalidateEffective() {
        businessLicenseMapper.revalidateEffective();
    }
}
